/* $Id: buffer.hpp jjs $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/
#ifndef __BUFFER_H
#define __BUFFER_H

/** \file buffer.h
    \brief A buffer for all input and output

    This buffer will send data on while new data is added to it.
*/

/// Buffer that collects data before transmission or reading
/**
 * A buffer that accepts data written to it even during transmitting data from another
 * part of the buffer.
*/
#include <string>

class buffer {
	private:

	char* data_;    ///< Pointer to the claimed buffer that can expand during the game
	unsigned long size_;     ///< The current size of the claimed buffer
    unsigned long read_pos_; ///< The current point inside the buffer till where the reading process has run
    unsigned long last_pos_; ///< The begin inside the data of the last writing where the reading will finish
    unsigned long write_pos_;///< The position where the writing to the buffer is now

	/// Get debug data from the buffer, doesn't change anything in the buffer
	std::string debug_(int pos, int len);

	public:
	buffer(void);
	~buffer(void);

	/// Write a specific string to the buffer.
	/**
	* Beware that this function doesn't write trailing zero's after strings.
	*/
	void write(const std::string data);
	/// Write the buffer with the contents of a file, return 1 when error
	int fwrite(int fd);

	/// Claim space in the buffer to be able to write data to it.
	char* writing(int bytes);

	/// Tell that you actual wrote this amount of data.
	void wrote(int bytes);

	/// Finished a set of data to send.
	char *send(long *length);

	/// Number of bytes still the buffer to read.
	unsigned long bytes(void);
	/// Pointer to the current read position
	char* ptr(void);
	/// Tell what part of the data you managed to read at this time
	void done(unsigned long bytes);
};

#endif // __BUFFER_H
